package com.cw.jeeyt.config;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.cw.jeeyt.service.converter.json.LoggingFromHttpMessageConverter;
import com.cw.jeeyt.service.interceptor.CrossInterceptor;
import com.cw.lang.common.framework.feign.RpcLoggingFeignInterceptor;
import com.cw.lang.common.framework.handle.JsonArgumentRequestMappingHandler;
import com.cw.lang.common.framework.inteceptor.ControllerFlagMvcHandlerInterceptor;
import com.cw.lang.common.framework.okhttp.LocalInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author chenw
 * @version $$Id: WebMvcConfiguration.java, v 0.1 2018/11/1 9:28 AM chenw Exp $$
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new JsonArgumentRequestMappingHandler());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
            .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //        registry.addResourceHandler("/modler/**")
        //                .addResourceLocations("classpath:/modler/");
    }
    @Bean
    public HandlerInterceptor getCrossInterceptor(){
        return new CrossInterceptor();
    }
    // @Bean
    // public HandlerInterceptor getInOutLoggingMvcHandlerInterceptor(){
    //     return new InOutLoggingMvcHandlerInterceptor();
    // }
    @Bean
    public HandlerInterceptor getControllerFlagMvcHandlerInterceptor(){
        return new ControllerFlagMvcHandlerInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getControllerFlagMvcHandlerInterceptor())
            .addPathPatterns("/**");
    }

    @Bean
    public HttpMessageConverter httpMessageConverter(ObjectMapper objectMapper) {
        return new LoggingFromHttpMessageConverter(objectMapper);
    }

    @Bean
    public LocalInterceptor rpcLoggingFeignInterceptor() {
        return new RpcLoggingFeignInterceptor();
    }

}
